package cn.edu.zjicm.wordsnet_d.data.huanbo;

import java.util.List;

import android.content.Context;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;

public class HuanboListManager1 {
	List<OneWord> words;
	int count = -1;
	public boolean repeating;
	OneWord curWordItem = null;
	final int REPEATING_COUNT_THRESHOLD = 2;
	final int INSERT_POSITION[] = { 0, 4, 8, 16, 32, 64 };
	// final int HUANBO_GROUP_SIZE = 10; //环播是每组15个单词
	Context mContext;
	WordFactory wf;

	public static int INTERVAL_DISPLAY_AFTER_VOCIE_PLAY = 1000;
	public static int INTERVAL_PER_WORD = 3000;

	int unRepeatWordCount = 0; // 非重复的单词个数
	boolean firstRound = true;

	public HuanboListManager1(List<OneWord> wordsList, Context mContext) {
		words = wordsList;
		wf = WordFactory.getInstance(mContext);
	}

	public void resetHuanboListManager1(List<OneWord> wordsList) {
		words = wordsList;
		firstRound = true;
		unRepeatWordCount = 0;
		count = -1;
	}

	public boolean next() {
		if (curWordItem != null) {
			words.remove(0);
			if (!repeating) {
				if (curWordItem.getRepeateTime() > 0) {
					if (curWordItem.getReadTime() < curWordItem.getRepeateTime()) {
						insertCurWord();
					} else if (curWordItem.getReadTime() == curWordItem.getRepeateTime()) {
						curWordItem.setRepeateTime(0);
						curWordItem.setReadTime(0);
						if (curWordItem.getReadTimeNotRepeat() < 3)
							words.add(curWordItem);
						// 更新听音时间
						updateCurWord();
					}
				} else {
					curWordItem.addReadTimeNotRepeat();
					if (curWordItem.getReadTimeNotRepeat() < 3) // 读过次数小于3，加入队尾等待读下一次；3次没有复读，并从环播列表清除
						addWord(words.size(), curWordItem);
					// 更新听音时间
					updateCurWord();
				}
			} else { // 复读状态
				addWord(words.size(), curWordItem);
			}
		}
		if (words.size() == 0) {
			curWordItem = null;
			return false;
		}
		curWordItem = words.get(0);
		return true;
	}

	public void pre() {
		if (words.size() == 0)
			return;
		curWordItem = words.get(words.size() - 1);
		if (curWordItem != null) {
			words.remove(curWordItem);
			words.add(0, curWordItem);
		}
	}

	int size() {
		return words.size();
	}

	public OneWord getCurWord() {
		return curWordItem;
	}

	public void addCurWordRepeatingCount() {
		curWordItem.setRepeatingCount(curWordItem.getRepeatingCount() + 1);
	}

	int getCurWordRepeatingCount() {
		return curWordItem.getRepeatingCount();
	}

	public void onEndRepeatOneWord() {
		curWordItem.setRepeateTime(2);
		changeCurWordVoiceFamiliarDegree(4);
		curWordItem.setReadTime(0);
		curWordItem.addReadTimeNotRepeat();
	}

	void insertCurWord() {
		curWordItem.setReadTime(curWordItem.getReadTime() + 1);
		addWord(INSERT_POSITION[curWordItem.getReadTime()], curWordItem);
	}

	void addWord(int location, OneWord word) {
		if (location < words.size())
			words.add(location, word);
		else
			words.add(word);
	}

	public void changeCurWordVoiceFamiliarDegree(int change) {
		if (curWordItem == null)
			return;
		curWordItem.setVoice_degree_fm(curWordItem.getVoice_degree_fm() + change);
	}

	void updateCurWord() {
		if (curWordItem == null)
			return;
		curWordItem.setLast_huanbo_time(DateTimeUtils.getDate());
		wf.markCurrentHuanboWord(curWordItem);
	}

}